Repository navigation
fix(verify): reject stale blocks and registry read failures - #14
Merged
Merged
Conversation
Since verification is pinned to one block (#11), a request can name a historical blockId. The controller then rebuilt the code deployed at that block and called set_contract with its older hash and height. The registry's set_contract does not compare heights, so this replaced the record for the code deployed now. The controller could not guard against that because it never knew whether a record existed: VerifierService.getContract returned null for every error, and the controller swallowed errors too, so an RPC outage looked like "no record" and led to a full rebuild, an IPFS pin and a second signed write. getContract now returns null only when get_contract answers JSON null (it returns Option), and throws a 503 when the registry can't be read or its answer isn't a record. The controller returns 503 with a retry message before any other read or build. With the record and the deployed hash at the pinned block known before cloning, the controller now: - returns the existing 400 "Code hash didn't change" when the record already holds the deployed hash, instead of after a Docker build; - refuses (500) when the record's block_height is not a safe non-negative integer or a canonical decimal string; - rejects (409) a pinned block below the record's block_height. The post-build unchanged-hash check stays as a backstop. nest/Dockerfile pins near-cli-rs to 0.31.1, the version production runs: the controller parses this CLI's output and relies on at-block-hash. Two jobs that finish in reverse order can still let the older one write last; fixing that needs the registry contract to compare heights. Co-Authored-By: Claude Opus 5.5 <[email protected]>
getContract decoded the RPC result with Buffer.from, which also accepts a string, so a malformed reply whose result was the text "null" decoded to JSON null and counted as an absent record. That skipped both pre-build guards and could lead to a clone, build, IPFS pin and write where a retryable 503 is required. A decoded object without a string code_hash was also returned as a record, so the unchanged-hash check fell through to a build and write. getContract now requires the result to be an array of bytes, and requires a returned record to have a non-empty string code_hash and a block_height that is a safe non-negative integer or a canonical decimal string. It returns block_height as a number. Anything else throws the same 503 as an RPC failure. The height parsing moved from the controller into the service, so a malformed record height is now a 503 instead of a 500. The absent-record controller test also asserts the success body. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Since #11 pinned verification to one block, a request can name a historical
blockId. If the code deployed at that block was verified earlier, the controller rebuilt it and calledset_contractwith the older hash and height. The registry'sset_contractreplaces the record without comparing heights, so the record for the code deployed now was replaced by an older one. After that the frontend shows a mismatch for a correctly verified contract, andget_contracts_by_code_hashno longer finds the current hash.The controller couldn't guard against this because it never reliably knew whether a record existed. Root cause:
VerifierService.getContractreturnednullfor every error, and the controller swallowed errors as well, so "registry unreachable" and "no record" were the same value. During an RPC outage an already-verified contract got a full rebuild, an IPFS pin and a second signed write.get_contractreturnsOption, so a genuinely absent record is JSONnull, which can be told apart from a failure.The unchanged-hash check also ran only after the Docker build, although #11 already reads the deployed hash at the pinned block before building.
Change
VerifierService.getContractreturnsnullonly whenget_contractanswers JSONnull. Its only caller is the verify controller. It throwsHttpException(503)in these cases:resultisn't an array of bytes (0–255), for example a string, sinceBuffer.fromwould otherwise decode a text"null"as an absent record;nullnor an object;code_hash;block_heightisn't a safe non-negative integer or a canonical decimal string, matching the strictness of the existing metadata/view_codechecks.It returns
block_heightas a number.POST /verify/rust:Could not read the verifier registry for <account>. Please retry later.before metadata, clone, build, pin or write.view_codereads are done (before cloning):code_hashequals the deployed hash, it returns the existing 400{"message": "Code hash didn't change"}.block_height, it returns 409Block <n> is older than the verified record for <account> at block <m>. Verify at block <m> or later.Equal and newer heights proceed as before.finality: final, not at the pinned block, because the guards compare against the record as it is now.nest/Dockerfile:cargo install near-cli-rs --version 0.31.1 --locked. The controller parses this CLI's output and relies onat-block-hash. 0.31.1 is what production runs and is the current crates.io stable (checked, not yanked).The request shape and the success response are unchanged.
Known follow-up (out of scope): two jobs for the same account that finish in reverse order can still let the older one write last. A check-then-write in the backend can't close that race. It needs
set_contractin the registry contract to reject ablock_heightbelow the stored one.This PR changes input validation, code-hash comparisons and the conditions for registry writes, so it needs maintainer review under AGENTS.md.
Validation
Run in a fresh OrbStack VM (Ubuntu 24.04, Node 22.23.5, no credentials), from
nest/, at057f26f:npm ci: OKnpm run lint: exit 0, and--fixleft no changes to the committed filesnpm test -- --runInBand: 12 suites, 113 tests passednpm run build: OKverify.controller.spec.tsandverifier.service.spec.ts:main'sverify.controller.tsandverifier.service.ts, 51 fail;5c2327f, 28 fail (the review findings: a stringresultcontaining"null", missing or invalidcode_hash, andblock_heightparsing in the service);057f26f.What the tests cover:
null) proceeds to 200, and the test checks themessageandchecksumbody;resultthat isn't a byte array (a string"null", a string record, an object, an array-like object, out-of-range or non-integer bytes), invalid JSON and non-object JSON;code_hash;block_height: missing,null, negative, fractional,1e400, unsafe integers,"01","1e3"," 1","0x10", booleans, arrays and objects;Not run: the production Docker image wasn't built, so the near-cli-rs pin is untested here. The staging replay is a separate later step.
🤖 Generated with Claude Code